package com.tian.print.depot;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tian.print.R;
import com.tian.print.app.BaseActivity;
import com.tian.print.app.NormalAdapter;
import com.tian.print.app.ViewHolder;
import com.tian.print.bean.CustomerMd;
import com.tian.print.http.BaseObserver;
import com.tian.print.http.RxSchedulers;
import com.tian.print.http.request.ApiRequest;

import java.util.ArrayList;

import butterknife.BindView;

public class SelectCustomerActivity extends BaseActivity {

    @BindView(R.id.rv_customer)
    RecyclerView rv_customer;
    private NormalAdapter<CustomerMd> adapter;
    private ArrayList<CustomerMd> customerMds;
    private CustomerMd selectCustomerMd;

    private String personCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_customer);
        showLeftBack();
        setTitleShow("请选择客户");

        personCode = getIntent().getStringExtra("personCode");
        customerMds = new ArrayList<>();
        adapter = new NormalAdapter<CustomerMd>(this, customerMds) {
            @Override
            protected void convert(ViewHolder holder, CustomerMd data) {
                TextView tvName = holder.getView(R.id.tv_name);

                if (data.isSelected()) {
                    tvName.setBackground(getResources().getDrawable(R.drawable.item_select_bg));
                    tvName.setTextColor(getResources().getColor(R.color.colorAccent));
                } else {
                    tvName.setBackground(getResources().getDrawable(R.drawable.item_normal_bg));
                    tvName.setTextColor(getResources().getColor(R.color.color_gray));
                }

                tvName.setText(data.getNativeName());

            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_customer_list;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<CustomerMd>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, CustomerMd data, int position) {

                for (CustomerMd item : customerMds) {
                    if (item.getCustomerCode().equals(data.getCustomerCode())) {
                        item.setSelected(!item.isSelected());
                    } else {
                        item.setSelected(false);
                    }
                }

                adapter.notifyDataSetChanged();
            }
        });

        rv_customer.setLayoutManager(new GridLayoutManager(this, 1));
        rv_customer.setAdapter(adapter);

        ApiRequest.apiClent.getCustomerMds(personCode).compose(RxSchedulers.compose())
                .subscribe(new BaseObserver<ArrayList<CustomerMd>>(this) {
                    @Override
                    public void onSuccess(ArrayList<CustomerMd> cmds) {
                        if (cmds == null || cmds.size() == 0)
                            return;

                        customerMds.addAll(cmds);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });

        setGoOnBtnListener("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (CustomerMd item : customerMds) {
                    if (item.isSelected()) {
                        selectCustomerMd = item;
                        break;
                    }
                }

                if (customerMds == null) {
                    Toast.makeText(SelectCustomerActivity.this, "请选择客户", Toast.LENGTH_LONG).show();
                    return;
                }

                Intent intent = getIntent();
                intent.putExtra("select_customer", selectCustomerMd);
                setResult(1122, intent);
                finish();
            }
        });
    }


}

